﻿using System;
using System.Collections.Generic;
using System.Security.Claims;
using System.Threading.Tasks;
using AspNet.Security.OAuth.GitHub;
using IGeekFan.FreeKit.Extras.Dependency;
using IGeekFan.FreeKit.Extras.FreeSql;
using LinCms.Common;
using LinCms.Data.Enums;
using LinCms.Entities;
using LinCms.IRepositories;

namespace LinCms.Cms.Users;

[DisableConventionalRegistration]
public class GithubOAuth2Serivice(IAuditBaseRepository<LinUserIdentity> userIdentityRepository,
        IUserRepository userRepository)
    : OAuthService(userIdentityRepository), IOAuth2Service
{
    /// <summary>
    /// 记录授权成功后的信息
    /// </summary>
    /// <param name="principal"></param>
    /// <param name="openId"></param>
    /// <returns></returns>
    public override async Task<long> SaveUserAsync(ClaimsPrincipal principal, string openId)
    {
        LinUserIdentity linUserIdentity = await userIdentityRepository.Where(r => r.IdentityType == LinUserIdentity.GitHub && r.Credential == openId).FirstAsync();

        long userId = 0;
        if (linUserIdentity == null)
        {
            string email = principal.FindFirst(ClaimTypes.Email)?.Value;
            string name = principal.FindFirst(ClaimTypes.Name)?.Value;
            string gitHubName = principal.FindFirst(GitHubAuthenticationConstants.Claims.Name)?.Value;
            string gitHubApiUrl = principal.FindFirst(GitHubAuthenticationConstants.Claims.Url)?.Value;
            string HtmlUrl = principal.FindFirst(LinConsts.Claims.HtmlUrl)?.Value;
            string avatarUrl = principal.FindFirst(LinConsts.Claims.AvatarUrl)?.Value;
            string bio = principal.FindFirst(LinConsts.Claims.Bio)?.Value;
            string blogAddress = principal.FindFirst(LinConsts.Claims.BlogAddress)?.Value;

            LinUser user = new()
            {
                Active = UserStatus.Active,
                Avatar = avatarUrl,
                CreateTime = DateTime.Now,
                LastLoginTime = DateTime.Now,
                Email = email,
                Introduction = bio + HtmlUrl,
                LinUserGroups = new List<LinUserGroup>()
                {
                    new()
                    {
                        GroupId = LinConsts.Group.User
                    }
                },
                Nickname = gitHubName,
                Username = "",
                BlogAddress = blogAddress,
                LinUserIdentitys = new List<LinUserIdentity>()
                {
                    new(LinUserIdentity.GitHub,name,openId,DateTime.Now)
                }
            };
            await userRepository.InsertAsync(user);
            userId = user.Id;
        }
        else
        {
            if (linUserIdentity.CreateUserId != null) userId = linUserIdentity.CreateUserId.Value;
        }

        return userId;
    }
}